﻿
//页面的类型常量及变量
var DAU_YESTERDAY=1;
var DAU_TOTAL=2;
var DAU_AVERAGE=3;
var DAU_MAU_RATE=4;
var vDAU=DAU_YESTERDAY;

//页面数据分类
var CHANNEL=1;
var VERSION=2;
var PERIOD=3;
var vChannelCode=CHANNEL;

//获取时间
var myDate=new Date();
var myYear=myDate.getFullYear();
var myMouth=(myDate.getMonth()+1)<10?("0"+(myDate.getMonth()+1)):(myDate.getMonth()+1);
var myDay=myDate.getDate()<10?("0"+myDate.getDate()):myDate.getDate(); 
console.log(myYear+"."+myMouth+"."+myDay+" DAU");

function onRightButton(e){
	switch(vDAU){
		case DAU_YESTERDAY:
			//console.log("onRightButton--DAU_YESTERDAY");
			window.location.href="DAU15Days.html";
			break;
		case DAU_TOTAL:
			//console.log("onRightButton--DAU_TOTAL");
			break;
		case DAU_AVERAGE:
			//console.log("onRightButton--DAU_AVERAGE");
            window.location.href="DAU1Year.html";			
			break;
		case DAU_MAU_RATE:
				//console.log("onRightButton--DAU_MAU_RATE");
		//DAU15Days.html
		break;
	}
}



function getDAUType(){
	var thisURL = document.URL;   
	var  value =thisURL.split('?')[1];  
	vDAU= Number(value.split('=')[1]);  
	console.log("thisURL="+thisURL+",vDAU="+vDAU);		 
}

function onChannel(e){
	var channelId= e.getAttribute("id");
	e.className="select-style";
	console.log("channelId="+channelId)
	var theOtherElement= "";
	if(channelId=="left-title"){
		vChannelCode=CHANNEL;
		theOtherElement=document.getElementById("right-title");
	}else if(channelId=="right-title"){
		vChannelCode=VERSION;
		theOtherElement=document.getElementById("left-title");
	}
	theOtherElement.className="unselect-style";
	console.log("vChannelCode="+vChannelCode)
	
	var title=e.innerHTML;
	console.log("title:"+title);
	var tHeadTitle= document.getElementById("thead-left");
	tHeadTitle.innerHTML=title;		
	mRequest.send();
}

window.onload= function (){
    getDAUType();	
	mTitle.setTitleBar();
	//console.log(mRequest.requestUrl());
	mRequest.send();
	
	
	/*
	var myData = new Array(["360",173],["百度",158],["应用宝",150],["华为",239],["VOVO",63],["OPPO",239]);
	var colors=new Array();
	for(var i=0;i<myData.length;i++){
		colors[i]="#ff0000";
	}	
	var myChart = new JSChart('chart', 'bar');
	myChart.setDataArray(myData);
	myChart.colorizeBars(colors);
	myChart.setAxisColor('red');
	myChart.setAxisWidth(1);
	myChart.setAxisNameX('');
	myChart.setAxisNameY('');
	myChart.setAxisNameColor('#619CD2');
	myChart.setAxisNameFontSize(9);
	myChart.resize(331, 197);
	myChart.setGridColor('#5D5F5D');
	*/
	//myChart.draw();
}
/*****************数据请求及处理*******************/
var mRequest={
	requestUrl:function(){
		var url=addMethod(strUrlHost,"DAU");
		url=addToken(url);
		var optType=vDAU*10+vChannelCode;
		url=addOptionType(url,optType);
	    console.log("url:"+url);
		return url;
	},
	send:function(){
			sendRequest(this.requestUrl(), window.mData.process);
			console.log("send");		
	}
	
}
/******************数据处理及图表绘制*************************/
var mData={
	process:function (resultData){	
		var flag = resultData.success;	
		console.log("flag="+flag);		
		if(flag==1){
			var vData=resultData.data;			
			var type=vDAU*10+vChannelCode;
			switch(type){
			case 11:
			    console.log("-----------11------");
				mData.processDAUData(vData);
                break;	
			case 12:
			    console.log("-----------12------");
				mData.processDAUData(vData);
                break;	
			case 21:			
			    console.log("-----------21------");
				mData.processDAUData(vData);
                break;	
			case 22:
			    console.log("-----------22------");
				mData.processDAUData(vData);
                break;	
			case 23:
			    console.log("-----------22------");
					
                break;
			case 31:
			    console.log("-----------31------");
				mData.processDAUData(vData);
                break;	
			case 32:
			    console.log("-----------32------");
				mData.processDAUData(vData);
                break;	
			case 33:
			    console.log("----------33------");
                break;					
			}
		}
	},
	processDAUData:function(vData){
		console.log(vData.length);		
		var chanelName="";
		var dau=0;
		var chartDataArray= new Array();
		mHTML.clearRow();
		for(var i=0;i<vData.length;i++){
			var temp=vData[i];		
		    if(vChannelCode==CHANNEL){
				chanelName=vData[i].channel_name;
			}else if(vChannelCode==VERSION){
				chanelName=vData[i].client_version;
			} 
			dau=vData[i].DAU;
			console.log("chanelName="+chanelName+",dau="+dau);
			mHTML.addRow(chanelName,dau);
			chartDataArray[i]=[chanelName,dau/10000];		
		}
		mHTML.drawChart(chartDataArray);
		mHTML.drawRow();
	}	
}
/************图标绘制**************************/
var mHTML={
	strRow:"",
	clearRow:function(){
		mHTML.strRow="";
	},
	addRow:function(channelName,number){	
		mHTML.strRow+= "<tr><td>"+channelName+"</td><td>"+number/10000+"w</td></tr>";
		console.log(mHTML.strRow);		
	},
	drawRow:function(){
		var tbodyElement=document.getElementById("tBody");
		tbodyElement.innerHTML=mHTML.strRow;
	},
	drawChart:function(dataArray){
		var colors=new Array();
		for(var i=0;i<dataArray.length;i++){
			console.log(dataArray[i]);
			colors[i] = '#ff0000';
		}	
		var myChart = new JSChart('chart', 'bar');	
		myChart.setDataArray(dataArray);	
		myChart.colorizeBars(colors);
		myChart.setTitle('');
		myChart.setAxisNameX('');
		myChart.setAxisNameY('');	
		myChart.setAxisPaddingTop(0);
		myChart.setAxisPaddingLeft(20);	
		myChart.setTextPaddingBottom(8);	
		myChart.setBarSpacingRatio(50);
		myChart.resize(screen.width, 197);
		myChart.setGridColor('#5D5F5D');
		myChart.setGrid(true);
		myChart.draw();	
	}
}



/************标题栏**************************/
var mTitle = {    
	setTitleBar:function(){		   	
			switch(window.vDAU){
			case  DAU_YESTERDAY:
				this.setTHeadRightTitle("昨日DAU");
				this.setTitle("昨日DAU");
				this.setTimetip(myYear+"."+myMouth+"."+myDay+" DAU");
				break;
			case  DAU_TOTAL:
				this.setTHeadRightTitle("本月累计DAU");			
				this.setTitle("本月累计DAU");
				this.setRightTitle("选择月份");
				this.setTimetip(myYear+"."+myMouth+" DAU");
				break;
			case  DAU_AVERAGE:
				this.setTHeadRightTitle("本月日均DAU");
				this.setTitle("本月日均DAU");
				this.setRightTitle("近一年趋势");
				this.setTimetip(myYear+"."+myMouth+" DAU");
				break;
			case  DAU_MAU_RATE:
				this.setTitle("DAU/MAU");
				this.setTHeadRightTitle("DAU/MAU");
				this.clearRightTitle();
				this.setTimetip(myYear+"."+myMouth+" DAU/MAU");
				break;
			default:					
				break;
		}
	},
	setTitle:function(title){
		var titleElement=document.getElementById("spanTitle");
		titleElement.innerHTML=title;		
	},
	setRightTitle:function(title){
		var btnElement=document.getElementById("buttonTitle");
		btnElement.value=title;
	},
	clearRightTitle:function(){
		var btnElement=document.getElementById("buttonTitle");
		btnElement.style.display="none";
	},
	setTHeadRightTitle:function(title){
		var tHeadRight=document.getElementById("thead-right");
		tHeadRight.innerHTML=title;
	},
	setTimetip:function(title){
		var tTimetip=document.getElementById("timetip");
		tTimetip.innerHTML=title;
	}
}


